<?php
declare (strict_types = 1);

namespace app\model;

use app\BaseController;
use think\Model;

/**
 * @property bool $recommend 推荐:0=否,1=是
 * @property int $browse 浏览数量
 * @property int $class_id 类目
 * @property int $collect 收藏数量
 * @property int $comment 评论数量
 * @property int $deletetime
 * @property int $hot 热度
 * @property int $id
 * @property int $issuer_id 媒体
 * @property int $like 点赞数量
 * @property int $users_id
 * @property string $adcode
 * @property string $content_s 内容(简体)
 * @property string $content_t 内容(繁体)
 * @property string $createtime
 * @property string $images 图片
 * @property string $title_s 主题(简体)
 * @property string $title_t 主题(繁体)
 * @property string $unique
 * @property string $url 链接
 * @property-read \app\model\NewsComment[] $comments
 * @property-read \app\model\NewsIssuer $issuer
 * @property-read \app\model\Users $users
 * @property-read \app\model\UsersCollect $users_collect
 * @property-read \app\model\UsersLike $users_like
 * @property-read mixed $content
 * @property-read mixed $title
 * @mixin \think\Model
 */
class News extends Model
{
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    public $lang;
    protected $append = [
        'title',
        'content',
    ];
//
//    public static function onBeforeInsert(Model $model): mixed
//    {
//        if (empty($model->getAttr('images'))){
//            $images = Config::where(['name'=>'news_images'])->value('value');
//            $model->setAttr('images',$images);
//        }
//        return true;
//    }
	
	function getImagesAttr($value)
	{
		if (empty($value)){
			return '';
		}
		$value = explode(',',$value);
		foreach ($value as &$v){
			if (!str_contains($v, "http")) {
				$v=config('app.static_host').$v;
			}
		}
		return implode(',',$value);
	}

    function comments()
    {
        return $this->hasMany(NewsComment::class);
    }

    function users()
    {
        return $this->belongsTo(Users::class);
    }
    function getTitleAttr($value,$data)
    {
        return $data['title'.BaseController::$lang];
    }

    function getContentAttr($value,$data)
    {
        return $data['content'.BaseController::$lang];
    }

    function issuer()
    {
        return $this->belongsTo(NewsIssuer::class,'issuer_id');
    }

    public function usersLike()
    {
        return $this->hasOne(UsersLike::class,'pro_id');
    }

    public function usersCollect()
    {
        return $this->hasOne(UsersCollect::class,'pro_id');
    }

}
